<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>通用后台管理系统</span>
      </div>
      <el-form
        :rules="rules"
        ref="ruleForm"
        label-position="left"
        label-width="60px"
        :model="form"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" round>登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { passRule } from "../utils/vaildate";
import { setToken } from "../utils/setToken";
import { getLogin } from "../api/login";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名是必填项", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        password: [{ validator: passRule, trigger: "blur", required: true }],
      },
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return this.$message.error("表单数据验证失败,请重新输入");
        const { data } = await getLogin(this.form);
        // console.log(data);
        setToken("token", data.token);
        setToken("username", data.username);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  background-color: skyblue;
  .box-card {
    width: 450px;
    margin: 200px auto;
    color: #fff;
    .el-form .el-form-item__label {
      color: #fff;
    }
    /deep/ .el-form-item__label {
      color: skyblue;
      font-weight: 700;
    }
    .el-card__header {
      font-size: 34px;
    }
    .el-button {
      width: 100%;
    }
    .clearfix {
      color: #000;
      font-size: 20px;
      text-align: center;
    }
  }
}
</style>